﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_PaymentDirectPetitions : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Payments pay = new B_FIM_Payments();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_FIM_Petitions pet = new B_FIM_Petitions();
        IBLL_FIM_PetitionDtls petdtls = new B_FIM_PetitionDtls();
        //IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        //IBLL_FIM_MAS_Methods m = new B_FIM_MAS_Methods();
        //IBLL_FIM_MAS_Events eve = new B_FIM_MAS_Events();
        //IBLL_FIM_PaymentCheques pc = new B_FIM_PaymentCheques();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                //lblBgmYear.Text = year.ToString();

                LoadDDLFilterBgmSourceSeq();
                LoadDDLPetitionSeq();
                ddlFilterBgmSourceSeq.SelectedIndex = 0;
                dpPayDate.SetValue(DateTime.Today);

                UpdateUI1("ViewAll");
            }
        }

        protected void LoadRadGrid1()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            RadGrid1.DataSource = pay.GetFull(strBgmSourceSeq, strBgmYear, null, null, "1");
        }

        protected void LoadDDLFilterBgmSourceSeq()
        {
            ddlFilterBgmSourceSeq.DataSource = source.GetFull(true);
            ddlFilterBgmSourceSeq.DataBind();
        }

        protected void LoadDDLPetitionSeq()
        {
            LoadDDLPetitionSeq(null);
        }

        protected void LoadDDLPetitionSeq(string strPetitionSeq)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;

            ddlPetitionSeq.DataSource = pet.GetDirectPaymentList(strBgmSourceSeq, strBgmYear, strPetitionSeq);
            ddlPetitionSeq.DataBind();

            ddlPetitionSeq.Items.Insert(0, new RadComboBoxItem("- เลือกรายการ -", ""));
        }

        protected void LoadPetitionInfo()
        {
            string strPetitionSeq = ddlPetitionSeq.SelectedValue;
            if (strPetitionSeq.Length == 0)
            {
                RadGrid2.DataSource = null;
            }
            else
            {
                RadGrid2.DataSource = petdtls.GetFull(strPetitionSeq);
            }

            RadGrid2.DataBind();
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;
                    cmdApprove.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;
                    cmdApprove.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;
                    cmdApprove.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
            }
        }

        protected void ClearControl1()
        {
            ddlPetitionSeq.SelectedIndex = 0;
            ddlPetitionSeq_SelectedIndexChanged(null, null);
        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            //{
            //    e.Item.Selected = true;
            //    string strReceiveSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["receive_seq"].Text;
            //    Session["ReceiveSeq"] = strReceiveSeq;
            //    LoadRadGrid2(strReceiveSeq);
            //}
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void ddlFilterBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_FIM_Payments info = new M_FIM_Payments();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strPaySeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_seq"].Text;

                info.pay_seq = Convert.ToInt32(strPaySeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = main.ConvertNullDateTime(dtNow);

                pay.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdApprove_Click(object sender, EventArgs e)
        {
            M_FIM_Payments info = new M_FIM_Payments();
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;
            string strPaySeq = lblPaySeq.Text;
            info.pay_seq = Convert.ToInt32(strPaySeq);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);
            info.record_status = "A";

            pay.ChangeStatus(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void ddlPetitionSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadPetitionInfo();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
            LoadPetitionInfo();
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            string strPaySeq = lblPaySeq.Text;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strPetitionSeq = ddlPetitionSeq.SelectedValue;
            string strMethodSeq = "3";
            string strPayDate = dpPayDate.GetStringValue();
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            foreach (GridDataItem item in RadGrid2.Items)
            {
                string strMoneyReqSeq = main.WebString(item["money_req_seq"].Text);
                string strMoneyReqName = main.WebString(item["money_req_name"].Text);
                string strEventSeq = main.WebString(item["event_seq"].Text);
                string strPetitionDtlsSeq = main.WebString(item["petition_dtls_seq"].Text);
                string strContactSeq = main.WebString(item["contact_seq"].Text);
                string strAmount = main.WebString(item["amount_real"].Text);
                string strAmountTax = main.WebString(item["amount_tax"].Text);

                M_FIM_Payments info = new M_FIM_Payments();
                info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
                info.bgm_year = Convert.ToInt32(strBgmYear);
                info.work_area_seq = Convert.ToInt32("1");
                info.event_seq = main.ConvertNullInt(strEventSeq);
                info.pay_date = main.ConvertNullDateTime(strPayDate);
                info.real_pay_date = main.ConvertNullDateTime(strPayDate);
                info.pay_name = "จ่ายตรง " + strMoneyReqName;
                info.method_seq = main.ConvertNullInt(strMethodSeq);
                info.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
                info.petition_dtls_seq = main.ConvertNullInt(strPetitionDtlsSeq);
                info.contact_seq = main.ConvertNullInt(strContactSeq);
                info.amount = main.ConvertNullDouble(strAmount);
                info.tax = main.ConvertNullDouble(strAmountTax);

                pay.Insert(info);
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {

        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {

        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")
            {
                if (ddlPetitionSeq.SelectedIndex >= 0)
                {
                    LoadPetitionInfo();
                }
            }
        }

        //==========================================================================
    }
}
